<template>
	<!--新增对话框-->
	<el-dialog
		:title="editForm.id ? '编辑运营信息' : '新增运营信息'"
		:visible.sync="dialogVisible"
		width="600px"
		:before-close="handleClose"
	>
		<el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">
			<el-form-item label="位置代码" prop="position_code" label-width="100px">
				<el-input v-model="editForm.position_code" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="位置名称" prop="position_name" label-width="100px">
				<el-input v-model="editForm.position_name" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="位置子代码" prop="position_sub_code" label-width="100px">
				<el-input v-model="editForm.position_sub_code" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="位置子名称" prop="position_sub_name" label-width="100px">
				<el-input v-model="editForm.position_sub_name" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="平台" prop="platform" label-width="100px">
				<el-select v-model="editForm.platform" placeholder="请选择">
					<el-option v-for="item in platformList" :key="item.label" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="版本选择" prop="ver_condition" label-width="100px">
				<div style="display: flex">
					<el-select v-model="editForm.ver_condition" placeholder="请选择">
						<el-option v-for="item in verList" :key="item.label" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
					<el-input-number
						v-model="editForm.app_version_id"
						v-if="editForm.ver_condition !== 'ALL'"
						style="width: 200px"
						autocomplete="off"
					></el-input-number>
				</div>
			</el-form-item>
			<el-form-item label="是否生效" prop="enable" label-width="100px">
				<el-radio-group v-model="editForm.enable">
					<el-radio :label="0">不生效</el-radio>
					<el-radio :label="1">生效</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="submitForm('editForm')">立即创建</el-button>
				<el-button @click="resetForm('editForm')">重置</el-button>
			</el-form-item>
		</el-form>
	</el-dialog>
</template>

<script>
export default {
	name: 'AddUpdateRole',
	data() {
		return {
			editForm: {},
			editFormRules: {
				position_code: [{ required: true, message: '请输入位置代码', trigger: 'blur' }],
				position_name: [{ required: true, message: '请输入位置名称', trigger: 'blur' }],
				position_sub_code: [{ required: true, message: '请输入位置子代码', trigger: 'blur' }],
				position_sub_name: [{ required: true, message: '请输入位置子名称', trigger: 'blur' }],
				platform: [{ required: true, message: '请选择平台', trigger: 'blur' }],
				ver_condition: [{ required: true, message: '请选择版本', trigger: 'blur' }],
				enable: [{ required: true, message: '请选择是否生效', trigger: 'blur' }],
			},
			dialogVisible: false,
			platformList: [
				{ value: 'COMMON', label: '公用' },
				{ value: 'ANDROID', label: 'Android' },
				{ value: 'IOS', label: 'ios' },
			],
			verList: [
				{ value: 'EQ', label: '等于' },
				{ value: 'GT', label: '大于' },
				{ value: 'GE', label: '大于等于' },
				{ value: 'LT', label: '小于' },
				{ value: 'LE', label: '小于等于' },
				{ value: 'ALL', label: '所有版本' },
			],
		};
	},
	methods: {
		resetForm(formName) {
			this.$refs[formName].resetFields();
			this.dialogVisible = false;
			this.editForm = {};
		},
		handleClose() {
			this.resetForm('editForm');
		},
		submitForm(formName) {
			this.$refs[formName].validate((valid) => {
				if (valid) {
					console.log(this.editForm);
					this.$axios.post('/notice/' + (this.editForm.id ? 'update' : 'add'), this.editForm).then((res) => {
						this.$message({
							showClose: true,
							message: '恭喜你，操作成功',
							type: 'success',
							onClose: () => {
								this.$emit('refreshDataList');
							},
						});
						this.dialogVisible = false;
						this.resetForm(formName);
					});
				} else {
					console.log('error submit!!');
					return false;
				}
			});
		},
		init(item) {
			this.dialogVisible = true;
			this.editForm = JSON.parse(JSON.stringify(item));
		},
	},
};
</script>

<style scoped></style>
